// ignore_for_file: must_be_immutable

/*
 * Author: Alvin
 * Modified By: Alvin
 * Created Date: 2023-07-20 17:55:01
 * Last Modified: 2023-07-25 17:41:55
 * Description:
 */

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/pages/tasks/task_list/task_list_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/func_utils.dart';

class ListPageItemView extends StatelessWidget {
  ListPageItemView({required this.model, required this.onItemTaped, super.key});

  TaskListModel model = TaskListModel();

  final void Function(TaskListModel model) onItemTaped;

  @override
  Widget build(BuildContext context) {
    var labelWidth = FuncUtils.getWidthByLocal(75.w, 140.w);
    String? label = model.getStatus();

    Color color;

    if (label == ResKey.pending.tr || label == ResKey.waitingForProcessing.tr) {
      color = const Color(0xFFFFF8EE);
    } else if (label == ResKey.processing.tr || label == ResKey.repairing.tr) {
      color = const Color(0xFFE6F5FF);
    } else if (label == ResKey.completed.tr || label == ResKey.finished.tr) {
      color = const Color(0xFFE7FBF3);
    } else if (label == ResKey.toBeAllocated.tr ||
        label == ResKey.repaired.tr) {
      color = const Color(0xFFFFF0F0);
    } else {
      color = Colour.white;
    }
    return GestureDetector(
      child: Container(
        // padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 16.w),
        margin: EdgeInsets.only(bottom: 10,left: 16.w,right: 16.w),
        decoration: BoxDecoration(
          color: Colour.white,
          borderRadius: BorderRadius.circular(8),
          border: Border.all(color: Colour.borderColor, width: 0.5)

        ),
        child: Column(
          children: [
            Container(
              // margin: EdgeInsets.only(bottom: 12,),
              height: 48,
              decoration: BoxDecoration(
                color: Colour.white,
                borderRadius: const BorderRadius.only(topLeft: Radius.circular(8),topRight: Radius.circular(8)),
                gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [
                    color,
                    Colors.white,

                  ],
                ),
              ),
              child: Row(

                children: [
                  Gaps.hGap16,
                  Expanded(
                      child: Text(
                    model.siteName ?? '',
                    style: TextStyle(
                        color: Colour.mainTextColor,
                        fontSize: 18.sp,
                        fontWeight: FontWeight.w500),
                    overflow: TextOverflow.ellipsis,
                  )),
                  Padding(
                    padding: EdgeInsets.only(left: 10.w),
                    child: statusWidget(),
                  ),
                  Gaps.hGap16,
                ],
              ),
            ),
            Container(margin: EdgeInsets.only(left: 16.w,right: 16.w),height:0.5,color: Colour.dividerColor,),
            Gaps.vGap12,
            ...model.infoLists.map(
              (e) => Padding(
                padding: EdgeInsets.only(bottom: 8.h),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Gaps.hGap16,
                    SizedBox(
                      width: labelWidth,
                      child: Text(e.label, style: TextStyles.lf14),
                    ),
                    Expanded(
                      child: Text(
                        e.value,
                        maxLines: 3,
                        style: TextStyles.f14,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Gaps.hGap16,
                  ],
                ),
              ),
            ),
            Gaps.vGap8
          ],
        ),
      ),
      onTap: () => onItemTaped(model),
    );
  }

  Widget statusWidget() {
    String? label = model.getStatus();

    Color color;

    if (label == ResKey.pending.tr || label == ResKey.waitingForProcessing.tr) {
      color = const Color(0xFFF69531);
    } else if (label == ResKey.processing.tr || label == ResKey.repairing.tr) {
      color = const Color(0xFF0F6EFF);
    } else if (label == ResKey.completed.tr || label == ResKey.finished.tr) {
      color = const Color(0xFF00AE66);
    } else if (label == ResKey.toBeAllocated.tr ||
        label == ResKey.repaired.tr) {
      color = const Color(0xFFFA3F3F);
    } else {
      color = Colour.labelTextColor;
    }

    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4),  color: color.withOpacity(0.1),),
      height: 22,
      padding: EdgeInsets.symmetric(horizontal: 4.h),

      child: Text(
        label ?? ResKey.unknownState.tr,
        style: TextStyles.styleWith(12.sp, color: color),
      ),
    );
  }
}
